package lib_dal.Connection;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectMSSQL {
	
	private String driver_name = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private Connection _connection;
	private Statement _statement;
	private PreparedStatement _preparedStatement;
	private CallableStatement _callableStatement;

	public ConnectMSSQL() {
		try {
			loadDriver(driver_name);
		} catch (ClassNotFoundException e) {
			System.out.println(e);
		}
	}

	/**
	 * @param driver
	 *            LOAD DRIVER.
	 * @throws ClassNotFoundException
	 */
	private void loadDriver(String driver) throws ClassNotFoundException {
		Class.forName(driver);
	}

	/**
	 * @return THUC HIEN QUERY TRUC TIEP
	 * @throws SQLException
	 */
	public Statement createStatement() throws SQLException {
		_connection = DriverManager.getConnection(GetConnectionString());
		_statement = _connection.createStatement();
		return _statement;
	}

	/**
	 * @param sql
	 * @return THUC HIEN DANG THAM SO
	 * @throws SQLException
	 */
	public PreparedStatement preparedStatement(String sql) throws SQLException {
		_connection = DriverManager.getConnection(GetConnectionString());
		_preparedStatement = _connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
		return _preparedStatement;
	}

	/**
	 * @param sql
	 * @return GOI STORE .
	 * @throws SQLException
	 */
	public CallableStatement callStatement(String sql) throws SQLException {
		_connection = DriverManager.getConnection(GetConnectionString());
		_callableStatement = _connection.prepareCall(sql);
		return _callableStatement;
	}

	/**
	 * @param query
	 * @return LAY RESULTSET
	 * @throws SQLException
	 */
	public ResultSet execQuery(String query) throws SQLException {
		createStatement();
		return _statement.executeQuery(query);
	}

	/**
	 * @param updateQuery
	 * @return UPDATE . RETURN SO DONG THAY DOI
	 * @throws SQLException
	 */
	public int execUpadate(String updateQuery) throws SQLException {
		createStatement();
		return _statement.executeUpdate(updateQuery);
	}

	private String GetConnectionString() {
		return this.server + ";databaseName=" + this.database + ";user="
				+ this.username + ";password=" + this.password;
	}

	/**
	 * CLOSE CONNECTION
	 */
	public void closeConnection() {
		try {
			if (_connection != null)
				_connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//private String server = "jdbc:sqlserver://192.168.1.3:1433";
	private String server = "jdbc:sqlserver://localhost:1433";
	private String database = "Project_AAL";
	private String username = "sa";
	private String password = "1234567";
	public static void main(String[] args) {
		ConnectMSSQL c = new ConnectMSSQL();
		System.out.println(c.GetConnectionString());
	}
	
}
